﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using com.paypal.soap.api;
using ASPDotNetSamples;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Data.SqlClient;
using System.Net;



public partial class UserControl_Subscribe : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string script =string.Format( "<script>var btnClientId='{0}';</script>",btnSubmit.ClientID);
        this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "variables", script);

        txtEmail.Attributes.Add("OnKeyPress", "ProcessKeyPress()");
        //if (!IsPostBack)
        //{
        //    BindCity();
        //}

    }

    //private void BindCity()
    //{
    //    var sub = new DealesDataContext();
    //    var query = sub.City_ReturnForSubscribe();
    //    ddlCity.DataTextField = "City";
    //    ddlCity.DataValueField = "CityID";
    //    ddlCity.DataSource = query;
    //    ddlCity.DataBind();
    //    ddlCity.Items.Insert(0, new ListItem("-Select", "0"));
    //}
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            int defaultCityID = int.Parse(ConfigurationManager.AppSettings["DefaultCityID"]);
            
            var sub = new DealesDataContext();
            Session["SubscribeDaialyDeal"] = "1"; 
            DateTime UnSubScribeDate = Convert.ToDateTime("1/1/2020");
            var query = sub.Insert_Subscribe(Convert.ToInt64(defaultCityID), txtEmail.Text, true, Convert.ToDateTime(System.DateTime.Now), Convert.ToDateTime(UnSubScribeDate));          
            Response.Cookies.Clear();
            Response.Cookies["CityID"].Value = Convert.ToString(defaultCityID);
            Response.Cookies["CityID"].Expires = DateTime.Now.AddDays(30);
           // Response.Cookies["CityName"].Value = ddlCity.SelectedItem.Text.ToString();
            Response.Cookies["CityName"].Expires = DateTime.Now.AddDays(30);

            var query1 = sub.Select_DealByCityIDNew(Convert.ToInt32(defaultCityID));
            int DealID = 0;
            foreach (var item in query1)
            {
                DealID =Convert.ToInt32(item.DealID);   
            }
            WebRequest objRequest = System.Net.HttpWebRequest.Create("http://www.zupons.com/Email.aspx?DID=" + DealID);
            StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
            string result = sr.ReadToEnd();
            sr.Close();
            string html = result;

            //System.Collections.Specialized.ListDictionary replacements = new System.Collections.Specialized.ListDictionary();
            //replacements.Add("<%City%>", ddlCity.SelectedItem.Text.ToString());
            //replacements.Add("<%Email%>", txtEmail.Text.ToString());
            //replacements.Add("<%MailSubject%>", html.ToString());

            //System.Net.Mail.MailMessage confirmation = Mailer.CreateMessage(new System.Web.UI.Control() as WebControl,
            //    Mailer.MessageTemplate.SubscribeUser, replacements, txtEmail.Text.ToString(), "Subscride User from Local");
            //Mailer.InternalSend(confirmation, false);
     
            Response.Redirect("~/Default.aspx?DealID=" + DealID);
        }
        catch(Exception ex)
        {
        }
    }
}
